#coding=utf-8
__author__ = 'liubin'

from project.tools import Base
from sqlalchemy import Column, DateTime, Integer, SmallInteger, String, BigInteger, ForeignKey, Boolean, Text, Numeric
from sqlalchemy.orm import relationship, backref
from sqlalchemy.sql.expression import text
from project.tools import JsonpickleMixin
from base import BaseModel

class PurchaseOrder(BaseModel, Base):
    """
    采购订单表
    """
    __tablename__ = 't_purchase_order'
    no = Column('no', String(50), nullable=False, doc=u'订单号')
    orderDate = Column('order_date', DateTime, server_default=text('NOW()'), doc=u'收货日期')
    supplierId = Column('supplier_id', BigInteger, ForeignKey('m_supplier.id'), nullable=False, doc=u'供应商ID')
    supplier = relationship("Supplier", foreign_keys=[supplierId])
    amount = Column('amount', Numeric, nullable=False, default=0, server_default='0', doc=u'金额')
    comment = Column('comment', Text, doc=u'备注')
    status = Column('status', SmallInteger, nullable=False, default=0, server_default='0', doc=u'状态')

    def __init__(self, data=None):
        """
        初始化
        :param data:
        :return:
        """
        if data:
            self.no = data.get('no')
            self.supplierId = data.get('supplierId')
            self.orderDate = data.get('orderDate')
            self.comment = data.get('comment')

class PurchaseOrderDetail(BaseModel, Base):
    """
    采购订单明细
    """
    __tablename__ = 't_purchase_order_detail'
    orderId = Column('order_id', BigInteger, ForeignKey('t_purchase_order.id'), nullable=False, doc=u'订单ID')
    order = relationship("PurchaseOrder", backref="detail", foreign_keys=[orderId])
    categoryId = Column('category_id', BigInteger, ForeignKey('m_category.id'), nullable=False, doc=u'布料种类ID')
    category = relationship("Category", foreign_keys=[categoryId])
    pileHeight = Column('pile_height', Numeric, nullable=False, default=0, server_default='0', doc=u'毛高')
    fabricWidth = Column('fabric_width', Numeric, nullable=False, default=0, server_default='0', doc=u'门幅')
    price = Column('price', Numeric, nullable=False, default=0, server_default='0', doc=u'单价')
    count = Column('count', Numeric, nullable=False, default=0, server_default='0', doc=u'数量')
    amount = Column('amount', Numeric, nullable=False, default=0, server_default='0', doc=u'金额')
    comment = Column('comment', Text, doc=u'备注')
    status = Column('status', SmallInteger, nullable=False, default=0, server_default='0', doc=u'状态')

    def __init__(self, data=None):
        """
        初始化
        :param data:
        :return:
        """
        if data:
            self.orderId = data.get('orderId')
            self.categoryId = data.get('categoryId')
            self.pileHeight = data.get('pileHeight')
            self.fabricWidth = data.get('fabricWidth')
            self.price = data.get('price')
            self.count = data.get('count')
            self.comment = data.get('comment')
            self.status = data.get('status')